package mapset;

import java.util.*;

/**
 * Created with IDEA
 * author:ju bai
 * Date:2021/2/24
 * Time:16:23
 **/
public class TestDemo {
    //1.List当中存10 0000个数据，找出第一个重复的数据
    //思路：遍历list 拿到每个元素之后 存到set
    //注意：存储之前，需要看set中是否有这个数据？有 就说明找到了第一个重复的数据
    
    public static int findFirstRepeatNumber() {
        Random random = new Random();
        ArrayList<Integer> list = new ArrayList<>();
        for (int i = 0; i < 10_0000; i++) {
            list.add(random.nextInt(10_0000));
        }
        HashSet<Integer> set = new HashSet<>();
        //contains
        for (int i = 0; i < 10_0000; i++) {
            if(!set.contains(list.get(i))) {
                set.add(list.get(i));
            }else {
                return list.get(i);
            }
        }
        return -1;
    }

    //2.把10 0000个数据当中的重复的数据 去重
    //思路：只需要把10W个数据放到集合当中

    public static Set<Integer> duplicates() {
        Random random = new Random();
        ArrayList<Integer> list = new ArrayList<>();
        for (int i = 0; i < 10_0000; i++) {
            list.add(random.nextInt(10_0000));
        }
        System.out.println(list);
        HashSet<Integer> set = new HashSet<>();
        //contains
        /*for (int i = 0; i < 10_0000; i++) {
            set.add(list.get(i));//刚好从小到大排序是"巧合"
        }*/
        set.addAll(list);
        return set;
    }

    //3.List当中存10 0000个数据，统计重复数字及其出现的次数

    public static void func() {
        Random random = new Random();
        ArrayList<Integer> list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            list.add(random.nextInt(10));
        }
        System.out.println(list);
        //遍历list 每次拿list当中的数据作为map的key
        HashMap<Integer,Integer> map = new HashMap<>();
        for (int i = 0; i < list.size(); i++) {
            Integer key = list.get(i);
            if(map.get(key) == null) {
                map.put(key,1);
            }else {
                Integer number = map.get(key);
                map.put(key,number+1);
            }
        }
        for (Map.Entry<Integer,Integer> entry :map.entrySet()) {
            if(entry.getValue() > 1) {
                System.out.println(entry.getKey()+"="+entry.getValue());
            }
        }
    }

    public static void main(String[] args) {
        func();
        //System.out.println(findFirstRepeatNumber());
        //System.out.println(duplicates());
    }



    public static void main2(String[] args) {
        //set中不能存储相同的元素
        Set<Integer> set = new HashSet<>();
        set.add(10);
        set.add(2);
        set.add(21);
        set.add(12);
        set.add(null);
        System.out.println(set);
    }


    public static void main1(String[] args) {
        HashMap<String,Integer> map = new HashMap<>();
        Map<String,Integer> map2 = new HashMap<>();
        Map<String,String> map3 = new HashMap<>();
        map3.put("bit","c++");//注意：put是根据一个函数进行存放的
        map3.put(null,null);
        map3.put("gaobo","java");
        map3.put("hhhh","java2");
        map3.put(null,null);

        Set<Map.Entry<String, String>> set = map3.entrySet();
        for (Map.Entry<String, String> entry  : set) {
            System.out.println(entry.getKey() +" " +  entry.getValue());
        }


        /*String val = map3.getOrDefault("gaobo3","6666");
        System.out.println(val);

        System.out.println(map3);

        //集合：不能存放相同元素
        Set<String> set = map3.keySet();
        System.out.println(set);

        */


    }
}
